package com.jcode222.service.impl;

import com.jcode222.dao.IOrderDao;
import com.jcode222.dao.IStorageDao;
import com.jcode222.service.IStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.jcode222.entity.Order;
import com.jcode222.service.IOrderService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class OrderDoServiceImpl implements IOrderService {

    /**
     * orderDao.
     */
    private final IOrderDao orderDao;
    /**
     * storageDao.
     */
    private final IStorageService storageService;

    /**
     * init.
     *
     * @param orderDao user dao
     */
    public OrderDoServiceImpl(final IOrderDao orderDao,IStorageService storageService) {
        this.orderDao = orderDao;
        this.storageService = storageService;
    }

    @Override
    public List<Order> findList(Order order) {
        return orderDao.findList(order);
    }

    @Override
    public Order findById(Long id) {
        return orderDao.findById(id);
    }

    @Override
    public int deleteById(Long id) {
        return orderDao.deleteById(id);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return orderDao.deleteByIds(ids);
    }

    @Override
    public int update(Order user) {
        return orderDao.update(user);
    }

    @Override
    public int save(Order user) {
        return orderDao.save(user);
    }

    @Override
    public void createOrder(Order order) {
        orderDao.save(order);
        log.info("商品代码:{}，购买数量:{}。创建订单中...",order.getCommodityCode(),order.getCount());
        storageService.decreaseStorage(order);
        log.info("库存扣减成功...");
    }

    @Transactional
    @Override
    public void createOrderTransaction(Order order) {
        orderDao.save(order);
        log.info("商品代码:{}，购买数量:{}。创建订单中...",order.getCommodityCode(),order.getCount());
        storageService.decreaseStorage(order);
        log.info("库存扣减成功...");
    }

    @Transactional
    @Override
    public void createOrderTransactionException(Order order) {
        orderDao.save(order);
        log.info("商品代码:{}，购买数量:{}。创建订单中...",order.getCommodityCode(),order.getCount());
        storageService.decreaseStorageRollback(order);
        log.info("库存扣减成功...");
    }

}
